package model.posterminal.company;

import java.util.ArrayList;

import model.address.Address;
import model.configuration.ConfigData;
import model.database.ObjectDataProvider;

import org.simpleframework.xml.*;

/**
 * Company
 * @author spotseluev
 *
 */
@Root(name = "company")
public class Company extends ObjectDataProvider{
	
	@Attribute( name = "id" )
	long id = ConfigData.NOVALUELONG; 				// id object in database
	
	@Element ( name = "companyname")
	String companyName;								// company name
	
	@Element ( name = "companyaddress")
	Address companyAddress;							// company address

	/**
	 * Get company name
	 * @return
	 */
	public String getCompanyName() {
		return companyName;
	}

	/**
	 * Set company name
	 * @return
	 */	
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	/**
	 * Get company address
	 * @return
	 */
	public Address getCompanyAddress() {
		return companyAddress;
	}

	/**
	 * Set company address
	 * @param companyAddress
	 */
	public void setCompanyAddress(Address companyAddress) {
		this.companyAddress = companyAddress;
	}

	/**
	 * Get object id in database
	 * @return
	 */
	public long getId() {
		return id;
	}
	
	/**
	 * Return company name
	 */
	@Override
	public String toString() {
		return companyName;
	}
	
	/**
	 * Save object in database
	 * @throws Exception
	 */
	public void save() throws Exception{
		final String storedProcedure = "companySave"; // stored procedure in database
		if (this.id == ConfigData.NOVALUELONG) {
			this.id = ((Company) this.sendObject2GetObject(this, this.getClass(), storedProcedure)).getId(); // send xml data and get xml data
		}
		else {
			this.sendObject2GetListObject(this, this.getClass(), storedProcedure);
		}
	}
	
	/**
	 * Load all company
	 * @return ArrayList of company
	 * @throws Exception
	 */
	public ArrayList<Company> getAllCompany() throws Exception {
		String storedProcedure = "companyListLoad";
		ArrayList<Company> result = new ArrayList<Company>();
		
		result = (ArrayList<Company>) this.sendObject2GetListObject(this, this.getClass(), storedProcedure);
		
		return result;
	}
	
	
	

}
